package com.lgy.core;


import com.lgy.comm.Constant;
import com.lgy.core.task.CleanTask;
import com.lgy.core.task.LeaderTask;
import com.lgy.core.task.MainTask;
import com.lgy.listener.MainListener;
import org.apache.log4j.Logger;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 定时任务加载工具类
 *
 * @author yuejing
 * @version V1.0.0
 * @date 2013-12-31 下午3:01:33
 */
@Component
public class TaskManager {

    private static final Logger LOGGER = Logger.getLogger(TaskManager.class);

    @Autowired
    private JobService jobService;

    @Autowired
    private LeaderTask leaderTask;

    @Autowired
    private MainTask mainTask;

    @Autowired
    private CleanTask cleanTask;

    public void init() {
        try {
            //添加-集群任务调度线程
            String mainCron = "0/20 * * * * ?";
            Constant.taskCronMap.put(Constant.TASK_ID_MAIN, mainCron);
            jobService.addJob(Constant.TASK_ID_MAIN, mainCron, mainTask, new MainListener(Constant.TASK_ID_MAIN));
        } catch (SchedulerException e) {
            LOGGER.error("添加系统的定时任务异常: " + e.getMessage(), e);
        }

        try {
            //添加-Leader的任务  configService.getValue(Config.LEADER_CRON, "0/5 * * * * ?")
            String leaderCron = "0/5 * * * * ?";
            Constant.taskCronMap.put(Constant.TASK_ID_LEADER, leaderCron);
            jobService.addJob(Constant.TASK_ID_LEADER, leaderCron, leaderTask, new MainListener(Constant.TASK_ID_LEADER));
        } catch (SchedulerException e) {
            LOGGER.error("添加-Leader的任务异常: " + e.getMessage(), e);
        }

        try {
            //添加-清除过期调度日志线程
            String cleanCron = "0/5 * * * * ?";
            Constant.taskCronMap.put(Constant.TASK_ID_CLEAN, cleanCron);
            jobService.addJob(Constant.TASK_ID_CLEAN, cleanCron, cleanTask, new MainListener(Constant.TASK_ID_CLEAN));
        } catch (SchedulerException e) {
            LOGGER.error("添加清除任务日志的定时任务异常: " + e.getMessage(), e);
        }
    }

}